<?php include("includes/session.php"); ?>
<?php include("includes/connect_db.php"); ?>
<?php include("includes/header.php"); ?>
<?php include("notif.php"); ?>

<style>
	.pilFormat{
		border: 1px solid #08c;
		margin-bottom: 5px;
		padding: 5px;
		word-break: break-word;
		cursor: pointer;
	}
	.filterAll{
		padding: 5px;
		color: #FFF;
		cursor: pointer;
		background-color: #08c;
	}
	.filterAll:hover{ background: #08c; }
	.filter{ width: 100px; }
	.btn{ width: 100px; }
	.pilihFilter{
		padding: 5px;
		border-bottom: 1px solid #08c;
	}
	.check{ cursor: pointer; }
	.kotakFilter{ 
		border: 1px solid #08c; 
		margin-bottom: 5px;
		max-height: 400px;
		overflow: auto;
	}
</style>

<div class="span9">  
    <h3>SMS</h3>                      
    <ul class="breadcrumb well">
        <li class="active">Privilege</li>                
    </ul>    
    <div class="row-fluid">
		<div id="format" class="span5">
			<?php
				$sql = "SELECT * FROM tb_operasi WHERE is_publik = 0";
				$query = mysql_query($sql);
				while ($tampil = mysql_fetch_array($query)) {
			?>
			<div class="pilFormat">
				<i class="icon-list-alt" style="opacity: 0.5;"></i>
				<span style="border-left: 10px;">
					<?php echo $tampil['nama_operasi']; ?>
				</span>
				<input type="hidden" class="idFormat" value="<?php echo $tampil['id_operasi']; ?>">
			</div>
			<?php
				}
			?>
		</div>
		<div id="kontak" class="span7">
		</div>
    </div>
</div><!--/span-->

<?php include("includes/footer.php"); ?>

<script type="text/javascript">
	var cek = 0;
	var tempView = '';
	var idKontak = '';
	var tipe = '';
	var index = '';
	var idFormat = '';

	$(document).ready(function() {
		$('.pilFormat').live('click', function(){
		 	idFormat = $(this).find('.idFormat').val();
			$('#kontak').slideUp();
			$('#kontak').slideDown(function(){
			 	$.ajax({
			 	  url: 'privilege-kontak.php',
			 	  type: 'POST',
			 	  data: {id: idFormat},
			 	  success: function(data, textStatus, xhr) {
			 	    $('#kontak').html(data);
			 	  }
			 	});
			});
		 });

    	$('.btnFilter').live('click', function(){
    		if ($(this).find('.checkRealAll').is(':checked')) {
    			
    		} else{
	    		if ($(this).find('.cek').val() == 0) {
	    			$('.btnFilter').find('.cek').val('0');
	    			$(this).closest('#kontak').find('.isiFilter').slideUp();
	    			$(this).parent('div').find('.isiFilter').slideDown();
	    			$(this).find('.cek').val('1');
	    			tipe = $(this).find('.checkRealAll').val();
	    		} else{
	    			$(this).parent('div').find('.isiFilter').slideUp();
	    			$(this).find('.cek').val('0');
	    		}
	    	}
			return false;
    	});

    	$('.checkAll').live('click', function(){
    		if($(this).parent('span').find('.checkRealAll').is(':checked')){
    			$(this).parent('span').find('.checkRealAll').removeAttr('checked');
				$(this).css('opacity', '0.4');
				tipe = $(this).parent('span').find('.checkRealAll').val();
				
				var formQuery = {
					id : idFormat,
					tipe : tipe
				};

				$.ajax({
				  url: 'act_privilege.php?act=2',
				  type: 'POST',
				  data: formQuery,
				  success: function(data, textStatus, xhr) {
				    $('.check').each(function(){
						var temp = $(this);
						if($(this).parent('div').find('.checkReal').is(':checked')){	
							$(this).parent('div').find('.checkReal').removeAttr('checked');
							$(this).css('opacity','0.4');
							$(this).parent('div').css('background-color', '#FFF');
						}
					});
				  }
				});
    		} else {
				$(this).parent('span').find('.checkRealAll').attr('checked', 'checked');
    			$(this).css('opacity', '1');
    			$(this).parent('span').parent('div').parent('div').find('.isiFilter').slideUp();
    			tipe = $(this).parent('span').find('.checkRealAll').val();
    			
    			var formQuery = {
					id : idFormat,
					tipe : tipe
				};

				$.ajax({
				  url: 'act_privilege.php?act=1',
				  type: 'POST',
				  data: formQuery,
				  success: function(data, textStatus, xhr) {
				    
				  }
				});
    		}
    	});

    	$('.check').live('click',function(){
			if($(this).parent('div').find('.checkReal').is(':checked')){
				cek--;
				$(this).parent('div').find('.checkReal').removeAttr('checked');
				$(this).css('opacity','0.4');
				$(this).parent('div').css('background-color', '#FFF');
				idKontak = $(this).parent('div').find('.checkReal').val();
				
				var formQuery = {
					id : idFormat,
					idKontak : idKontak,
					tipe : tipe
				};

				$.ajax({
				  url: 'act_privilege.php?act=4',
				  type: 'POST',
				  data: formQuery,
				  success: function(data, textStatus, xhr) {
				    
				  }
				});
			}else{
				cek++;
				$(this).parent('div').find('.checkReal').attr('checked','checked');
				$(this).css('opacity','1');
				$(this).parent('div').css('background-color','#EEE');
				idKontak = $(this).parent('div').find('.checkReal').val();
				
				var formQuery = {
					id : idFormat,
					idKontak : idKontak,
					tipe : tipe
				};

				$.ajax({
				  url: 'act_privilege.php?act=3',
				  type: 'POST',
				  data: formQuery,
				  success: function(data, textStatus, xhr) {
				    
				  }
				});
			}
		});

		$('.check').on({
            mouseenter:
               function()
               {
                    $(this).css('opacity','1');
               },
            mouseleave:
               function()
               {
                    if(!$(this).parent('div').find('.checkReal').is(':checked')){
                        $(this).css('opacity','0.4');
                    }       
               }
       	});

        $('#kontak').on('keyup', '.search-kontak', function(){
        	console.log("haha");
	        var keySearch = new RegExp($(this).val().toUpperCase());
	        $(this).closest('.kotakFilter').find('.searchNama').each(function(i, e){
	            var tempMenu = $(this).html().toUpperCase();
	            var menu = $(this).closest('div');
            	if (keySearch.test(tempMenu)) {
	                menu.show();
	            } else {
                	menu.hide();
	            }
	        });
	    });

	    /*$('#search-kontak').keyup(function(){
            var keySearch = new RegExp($(this).val().toUpperCase());
            $('.searchNama, .searchNomor').each(function(i, e){
                var tempMenu = $(this).html().toUpperCase();
                var menu = $(this).closest("tr");
                if (menu.find(".searchNama").data("found")=="0") {
                    if(keySearch.test(tempMenu)){
                        menu.show();
                        menu.find(".searchNama").data("found", 1);
                    }else{
                        menu.hide();
                    }
                };
                if ($(e).attr("class")=="searchNomor") {
                    menu.find(".searchNama").data("found", 0);
                };
            });
        });*/
    });
</script>